<?php 

require ("\php_rgr0.php"); 
require ("\php_procedures.php"); 
require ("\php_progresos.php"); 

$listHistoriales = Array("fecha" => "", "TABLE_NAME" => "", "TABLESPACE_NAME" => "", "NUM_ROWS" => 0,
        "MB" => 0, "nuevos" => 0);

function LlenaHistoriales($listHistoriales, $nomtabla, $Tablespace, $IndTotales, $tamTabla, $nuevos) {
    array_push($listHistoriales, Array("fecha" => date("Y-m-d H:i:s"), "TABLE_NAME" => $nomtabla, "TABLESPACE_NAME" => $Tablespace, "NUM_ROWS" => $IndTotales,
        "MB" => $tamTabla, "nuevos" => $nuevos));
}

function DatosPrueba() {

    $listHistoriales [] = Array("fecha" => date("Y-m-d H:i:s"), "TABLE_NAME" => "t1", "TABLESPACE_NAME" => "ventas", "NUM_ROWS" => 100,
        "MB" => 100, "nuevos" => 10);

    $listHistoriales [] = Array("fecha" => date("Y-m-d H:i:s"), "TABLE_NAME" => "t2", "TABLESPACE_NAME" => "Produccion", "NUM_ROWS" => 200,
        "MB" => 200, "nuevos" => 20);

    CreaXML($listHistoriales);
}

function CreaXML($listHistoriales) 
{
    $doc = new DOMDocument();
    $doc->formatOutput = true;

    $root = $doc->createElement("Bitacora");
    $doc->appendChild($root);

    foreach ($listHistoriales as $historial) {
        //Creo el encabezado
        $head = $doc->createElement("historial");

        $nametabla = $doc->createElement("TABLE_NAME");
        $nametabla->appendChild(
                $doc->createTextNode($historial['TABLE_NAME'])
        );
        $head->appendChild($nametabla);



        $nameTablespace = $doc->createElement("TABLESPACE_NAME");
        $nameTablespace->appendChild(
                $doc->createTextNode($historial['TABLESPACE_NAME'])
        );
        $head->appendChild($nameTablespace);



        $fecha = $doc->createElement("fecha");
        $fecha->appendChild(
                $doc->createTextNode($historial['fecha'])
        );
        $head->appendChild($fecha);


        $IndTotales = $doc->createElement("NUM_ROWS");
        $IndTotales->appendChild(
                $doc->createTextNode($historial['NUM_ROWS'])
        );
        $head->appendChild($IndTotales);


        $tamTabla = $doc->createElement("MB");
        $tamTabla->appendChild(
                $doc->createTextNode($historial['MB'])
        );
        $head->appendChild($tamTabla);


        $numNuevos = $doc->createElement("nuevos");
        $numNuevos->appendChild(
                $doc->createTextNode($historial['nuevos'])
        );
        $head->appendChild($numNuevos);


        $root->appendChild($head);
    }//TERMINA FOREEACH
    $doc->saveXML();
    $doc->save("bitacora.xml");
}

function CargaXML($listHistoriales) 
{   
    //DatosPrueba(Array());
    $rl = new RegresionLineal(); 
    $listHistorialesAux = Array("fecha" => "", "TABLE_NAME" => "", "TABLESPACE_NAME" => "", "NUM_ROWS" => 0,
        "MB" => 0, "nuevos" => 0);
    
    $myhistory = simplexml_load_file('bitacora.xml');
    foreach ($myhistory as $historyinfo):
//        $tabla = $historyinfo->TABLE_NAME;
//        $TABLESPACE_NAME = $historyinfo->TABLESPACE_NAME;
//        $date = $historyinfo->fecha;
//        $IndTotales = $historyinfo->NUM_ROWS;
//        $tamTotal = $historyinfo->MB;
//        $news = $historyinfo->nuevos;
        
        $listHistorialesAux["fecha"] = $historyinfo->fecha;
        $listHistorialesAux["TABLE_NAME"] = $historyinfo->TABLE_NAME;
        $listHistorialesAux["TABLESPACE_NAME"] = $historyinfo->TABLESPACE_NAME;
        $listHistorialesAux["NUM_ROWS"] = $historyinfo->NUM_ROWS;
        $listHistorialesAux["MB"] = $historyinfo->MB;
        $listHistorialesAux["nuevos"] = $historyinfo->nuevos;
        array_push($listHistoriales, $listHistorialesAux);
        //$rl->agrega($historyinfo->TABLESPACE_NAME, $historyinfo->TABLE_NAME, $historyinfo->nuevos, $historyinfo->MB);    
    endforeach;  
    
    calls($rl, $listHistoriales);    
    $x = 1;  
   // $xpath = new DOMXPath($doc);
   // $table  = $xpath->query('//table[@id="tablaPRC1"]')->item(0); 
    $ls = $rl->getNombreTablespaces();
    $comillas = '"';
    foreach ($ls as $n_)
    { 
        $used = $rl->getPorcUsado($n_);  
        $pron = $rl->getPronostico($n_);  
        $pr_fin="";
        if($pron == 0 && $used == 0) 
            $pr_fin = "Uncalculable";
        else
            $pr_fin = formatoPronostico($pron);
        echo "<tr>";
        echo    "<td onclick=".$comillas."MuestaGrafico('".$n_."')".$comillas.">".$n_."</td>";
        //. ""."'"."Graficando('".$n_."')'>".$n_."</td>"; //    Graficando('".$n_."')  
        echo    "<td> <div class='rows'>".progresando($used,$x)."&nbsp<span class = 'pron-span'>".$pr_fin.     
                "</span><button class='botPopup btPorgress' onclick=".$comillas."quitaRow(".$x++.",'".$n_."',".$used.",'".$pr_fin."')".$comillas."><img src='images/close_1.png'></img></button></div></td>"; 
        echo "</tr>";
    }
}
